package com.hz.pay.center.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.client.RestTemplate;

import java.util.UUID;

public class TestController {



    @Autowired
    private RestTemplate restTemplate; //单咧


    String url = "：https://api.mch.weixin.qq.com/pay/unifiedorder";

    String wechatAppId = "";
    String wechatMchId = "";
    String signType = "";



    public void test(String orderId, String amt){


//        <xml>
//   <appid>wx2421b1c4370ec43b</appid>
//   <attach>支付测试</attach>
//   <body>JSAPI支付测试</body>
//   <mch_id>10000100</mch_id>
//   <detail><![CDATA[{ "goods_detail":[ { "goods_id":"iphone6s_16G", "wxpay_goods_id":"1001", "goods_name":"iPhone6s 16G", "quantity":1, "price":528800, "goods_category":"123456", "body":"苹果手机" }, { "goods_id":"iphone6s_32G", "wxpay_goods_id":"1002", "goods_name":"iPhone6s 32G", "quantity":1, "price":608800, "goods_category":"123789", "body":"苹果手机" } ] }]]></detail>
//   <nonce_str>1add1a30ac87aa2db72f57a2375d8fec</nonce_str>
//   <notify_url>https://wxpay.wxutil.com/pub_v2/pay/notify.v2.php</notify_url>
//   <openid>oUpF8uMuAJO_M2pxb1Q9zNjWeS6o</openid>
//   <out_trade_no>1415659990</out_trade_no>
//   <spbill_create_ip>14.23.150.211</spbill_create_ip>
//   <total_fee>1</total_fee>
//   <trade_type>JSAPI</trade_type>
//   <sign>0CB01533B8C1EF103065174F50BCA001</sign>
//</xml>

        String nonceStr = UUID.randomUUID().toString().replace("-","");

        String sign = ""; //TODO 根据算法运算出来


        StringBuffer stringBuffer = new StringBuffer();

        stringBuffer.append("<xml>");
        stringBuffer.append("<appid>").append(wechatAppId).append("</appid>");
        stringBuffer.append("<mch_id>").append(wechatMchId).append("</mch_id>");
        stringBuffer.append("<device_info>").append(wechatAppId).append("</device_info>");
        stringBuffer.append("<nonce_str>").append(nonceStr).append("</nonce_str>");
        stringBuffer.append("<sign>").append(sign).append("</sign>");
        stringBuffer.append("<sign_type>").append(wechatAppId).append("</sign_type>");

        stringBuffer.append("<out_trade_no>").append(orderId).append("</out_trade_no>");

        //...


        stringBuffer.append("</xml>");

      String respXml = restTemplate.postForObject(url,stringBuffer.toString(),String.class);

//      <xml>
//   <return_code><![CDATA[SUCCESS]]></return_code>
//   <return_msg><![CDATA[OK]]></return_msg>
//   <appid><![CDATA[wx2421b1c4370ec43b]]></appid>
//   <mch_id><![CDATA[10000100]]></mch_id>
//   <nonce_str><![CDATA[IITRi8Iabbblz1Jc]]></nonce_str>
//   <openid><![CDATA[oUpF8uMuAJO_M2pxb1Q9zNjWeS6o]]></openid>
//   <sign><![CDATA[7921E432F65EB8ED0CE9755F0E86D72F]]></sign>
//   <result_code><![CDATA[SUCCESS]]></result_code>
//   <prepay_id><![CDATA[wx201411101639507cbf6ffd8b0779950874]]></prepay_id>
//   <trade_type><![CDATA[JSAPI]]></trade_type>
//</xml>


      //解析xml.


    }





}
